package com.example.fx.fgViewModel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.base.entity.GoodsEntityResult
import com.example.base.entity.IndexEntityResult
import com.example.base.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class IndexViewModel:BaseViewModel() {
    val indexSuccess = MutableLiveData<IndexEntityResult>()
    val indexFail = MutableLiveData<String>()
    //获取首页数据
    fun getIndex(){
        viewModelScope.launch {
            try {
                indexSuccess.value = apiService.getIndex()
            }catch (e:Exception){
                indexFail.value = e.message
            }
        }
    }

    val goodsSuccess = MutableLiveData<GoodsEntityResult>()
    val goodsFail = MutableLiveData<String>()
    //获取商品数据
    fun goodsIndex(type:Int,page:Int,limit:Int){
        viewModelScope.launch {
            try {
                goodsSuccess.value = apiService.goodsIndex(type, page, limit)
            }catch (e:Exception){
                goodsFail.value = e.message
            }
        }
    }
}